/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.j2meturk.main;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author eray(dot)ince(at)gmail(dot)com
 */
public class CustomGauge extends Canvas implements CommandListener{

    private Command backCommand = null;
    int w = 0, h = 0;
    Image indicator = null;
    double MaxValue = 0, MinValue = 0;
    int Value2atGauge = 0, Value3atGauge = 0;
    int GaugeWPoint = 0, GaugeHPoint = 0;
    double val = 0.0;
    Font font = null;
    Font font_Numbers = null;
    public static final int WitdhFree = 8;
    public static final int NumbersFree1W = 78;
    public static final int NumbersFree1H = 50;
    public static final int NumbersFree2W = 64;
    public static final int NumbersFree2H = 42;
    public static final int NumbersFree3W = 60;
    public static final int NumbersFree3H = 44;
    public static final int NumbersFree4W = 75;
    public static final int NumbersFree4H = 53;
    public static final int[] NumbersFreeForDigit = new int[3];

//     g.drawString(IV.elementAt(0).toString(), w/2-78, h/2+50, Graphics.BOTTOM|Graphics.LEFT);
//        g.drawString(IV.elementAt(1).toString(), w/2-62, h/2-40, Graphics.BOTTOM|Graphics.LEFT);
//        g.drawString(IV.elementAt(2).toString(), w/2+60, h/2-44, Graphics.BOTTOM|Graphics.RIGHT);
//        g.drawString(IV.elementAt(3).toString(), w/2+75, h/2+53, Graphics.BOTTOM|Graphics.RIGHT);
    public CustomGauge(MIDlet m, double MinVal, double MaxVal, double Value) throws Exception {
        if (MinVal > MaxVal || Value > MaxVal) {
            throw new Exception("Check Values");
        } else if (numDigits(MaxVal) > 3) {
            throw new Exception("Supports for 3 Digits");
        }

        this.val = Value;
        this.MaxValue = MaxVal;
        this.MinValue = MinVal;
        findgapValues();
        findLinePoints();
        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
        font_Numbers = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);

        //arrange numbers for digit count. arrangment has been designed for 3 digit number if it is more than 3 digit throws exception
        NumbersFreeForDigit[0] = -8;
        NumbersFreeForDigit[1] = 0;
        NumbersFreeForDigit[2] = 8;

        //
        backCommand = new Command("Back",Command.EXIT , 1);
        this.addCommand(backCommand);
        this.setCommandListener(this);
        try {
            w = getWidth();
            h = getHeight();
            indicator = Image.createImage("/org/j2meturk/res/indic.png");
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }

    }

    public static int numDigits(double num) {
        if (num < 10) {
            return (1);
        } else {
            return (1 + numDigits(num / 10));
        }
    }

    protected void paint(Graphics g) {
        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, w, h);

        g.drawImage(indicator, (w - indicator.getWidth()) / 2, (h - indicator.getHeight()) / 2, 16 | 4);
        g.setColor(0x00FFFFFF);
        g.drawLine(w / 2 - 2, h / 2 + 5, w / 2 + GaugeWPoint, h / 2 + GaugeHPoint);
        //   g.drawLine(w /2 -1, h / 2+1, w / 2-24, h / 2-24);
        g.setColor(0x001517CC);

        double Minus_Width = numDigits(val);
        g.setFont(font);
        g.drawString(String.valueOf(val),(int) (w / 2 - (WitdhFree + Minus_Width)), h / 2 + 40, 16 | 4);
        g.setFont(font_Numbers);
        //check 3 digit

        g.drawString(String.valueOf(MinValue), w / 2 - NumbersFree1W - NumbersFreeForDigit[numDigits(Double.parseDouble(String.valueOf(MinValue))) - 1], h / 2 + NumbersFree1H, Graphics.BOTTOM | Graphics.LEFT);
        g.drawString(String.valueOf(Value2atGauge), w / 2 - NumbersFree2W - NumbersFreeForDigit[numDigits(Double.parseDouble(String.valueOf(Value2atGauge))) - 1], h / 2 - NumbersFree2H, Graphics.BOTTOM | Graphics.LEFT);
        g.drawString(String.valueOf(Value3atGauge), w / 2 + NumbersFree3W + NumbersFreeForDigit[numDigits(Double.parseDouble(String.valueOf(Value3atGauge))) - 1], h / 2 - NumbersFree3H, Graphics.BOTTOM | Graphics.RIGHT);
        g.drawString(String.valueOf(MaxValue), w / 2 + NumbersFree4W + NumbersFreeForDigit[numDigits(Double.parseDouble(String.valueOf(MaxValue))) - 1], h / 2 + NumbersFree4H, Graphics.BOTTOM | Graphics.RIGHT);
    }

    private void findgapValues() {
        int X = 0;
        if (MinValue == 0) {
            X = (int) MaxValue / 3;
            Value2atGauge = X;
            Value3atGauge = X * 2;
        } else {
            X = (int) (MinValue + MaxValue) / 5;
            Value2atGauge = X * 2;
            Value3atGauge = X * 3;
        }

    }

    private void findLinePoints() {
        int[] WLines = new int[20];
        int[] HLines = new int[20];

        WLines[0] = -40;
        WLines[1] = -40;
        WLines[2] = -40;
        WLines[3] = -40;
        WLines[4] = -40;
        WLines[5] = -32;
        WLines[6] = -25;
        WLines[7] = -20;
        WLines[8] = -15;
        WLines[9] = 0;

        WLines[10] = +15;
        WLines[11] = +20;
        WLines[12] = +25;
        WLines[13] = +32;
        WLines[14] = +40;
        WLines[15] = +40;
        WLines[16] = +40;
        WLines[17] = +40;
        WLines[18] = +40;
        WLines[19] = +40;


        HLines[0] = +30;
        HLines[1] = +20;
        HLines[2] = +10;
        HLines[3] = 0;
        HLines[4] = -10;
        HLines[5] = -17;
        HLines[6] = -25;
        HLines[7] = -32;
        HLines[8] = +10;
        HLines[9] = -40;

        HLines[10] = -40;
        HLines[11] = -40;
        HLines[12] = -32;
        HLines[13] = -25;
        HLines[14] = -17;
        HLines[15] = -10;
        HLines[16] = 0;
        HLines[17] = +10;
        HLines[18] = +20;
        HLines[19] = +30;


        int i = 0;
        if (MinValue == 0) {
            i = (int) (val / (MaxValue / 20) - 1);
            if (i < 0) {
                i = 1;
            }
        } 
        else if(val == 0){
            i = 0;
        }
        else {

            i = (int) (val / ((MinValue + MaxValue) / 20))+1;
            if (i < 0) {
                i = 1;
            }
        }

        GaugeWPoint = WLines[i];
        GaugeHPoint = HLines[i];


    }

    public void commandAction(Command c, Displayable d) {
        if(c == backCommand){
            
        }
    }

    
}
